package com.gxw.project.web;
import com.gxw.project.core.Result;
import com.gxw.project.core.ResultGenerator;
import com.gxw.project.core.aop.LoggerManage;
import com.gxw.project.model.GxwUser;
import com.gxw.project.service.GxwUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxw.project.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.List;

/**
* Created by chenzhongqiang on 2017/09/20.
*/
@RestController
@RequestMapping("/gxw/user")
@Api(value = "用户中心")
public class GxwUserController {
    @Resource
    private GxwUserService gxwUserService;

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public Result add(GxwUser gxwUser) {
        gxwUserService.save(gxwUser);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        gxwUserService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(GxwUser gxwUser) {
        gxwUserService.update(gxwUser);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        GxwUser gxwUser = gxwUserService.findById(id);
        return ResultGenerator.genSuccessResult(gxwUser);
    }

    @PostMapping("/list")
    @LoggerManage(description = "获取列表")
    @ApiOperation(value = "获取用户列表", notes = "分页获取用户列表")
    public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        PageHelper.startPage(page, size);
        List<GxwUser> list = gxwUserService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @ApiOperation(value = "根据手机号码获取用户信息",notes = "前段校验手机号码是否正常")
    @ApiImplicitParam(name = "telephone",value = "手机号码",required = true,dataType = "String")
    @PostMapping("/findByTelephone")
    public Result findByUsername(@RequestParam String telephone) {
        Condition condition = new Condition(GxwUser.class);
        condition.createCriteria().andEqualTo("telephone", telephone);
        List<GxwUser> gxwUsers = gxwUserService.findByCondition(condition);
        return ResultGenerator.genSuccessResult(gxwUsers);
    }

}
